package no.ntnu.fp.gui;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTextField;

import run.Main;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.sql.SQLException;
import java.awt.Color;

public class AddMeetingRoom extends JFrame {
	
	private JTextField txtRoomName;
	private JTextField txtLocation;
	private JTextField txtCapacity;
	
	
	public AddMeetingRoom() {
		getContentPane().setBackground(Color.WHITE);
		setResizable(false);
		initialiseUI();
		setLocationRelativeTo(null);
	}
	
	public void createMeetingRoom() {
		String name = txtRoomName.getText();
		String location = txtLocation.getText();
		String strCapacity = txtCapacity.getText();
		
		if (!isRegistrationValid(name, location, strCapacity)) {
			return;
		}
		
		int capacity = Integer.parseInt(strCapacity);
		
		try {
			registerRoomInDatabase(name, location, capacity);
		}
		catch (Exception e) {
			showErrorMessage("The user could not be registered in the database. The error was: " + e.getMessage());
			return;
		}
		
		showSuccessMessage();
		hideForm();
	}
	
	private void hideForm() {
		this.setVisible(false);
	}
	
	private void showSuccessMessage() {
		JOptionPane.showMessageDialog(this, "The registration was successful!", "Confirmation", JOptionPane.INFORMATION_MESSAGE);
	}
	
	
	/**
	 * Register entered information in SQL database;
	 * @param name
	 * @param location 
	 * @param capacity
	 */
	private void registerRoomInDatabase(String roomName, String location, int capacity) {
	
		String query = "INSERT INTO `calendarsystem`.`meetingRoom` (" +
		"`meetingRoomId` ," +
		"`roomName` ," +
		"`location`,"+
		"`capacity`)" +
		"VALUES (NULL , '"+roomName+"', '"+location+"', '"+capacity+ "')";
		
			try {
				Main.dbConn.modifyDatabase(query);
			} catch (SQLException e) {
				e.printStackTrace();
			}		
		
	}
	
	private boolean roomAlreadyExists(String name) {
	
		boolean roomAlreadyExists = false; //we will (maybe?) check the database to see if the room already exists	
		
		return roomAlreadyExists;
		
	}
	
	private boolean isRegistrationValid(String name, String location, String strCapacity) {
		if (name.length() < 2) {
			showErrorMessage("The name field must be 2 or more characters.");
			return false;
		}	
		
		if (location.length() < 2) {
			showErrorMessage("The location field must be 2 or more characters."); //should the location be optional?
			return false;
		}
		
		try {
			int capacity = Integer.parseInt(strCapacity);

			if (capacity <= 0) {
				showErrorMessage("Capacity must be 1 or higher."); //if number entered is less than or equal to 0
				return false;
			}
		} 
		catch (NumberFormatException ex) {
			showErrorMessage("Capacity must be a valid number."); //if the input is not a number (if it does not parse)
			return false;
		}
		
		if (roomAlreadyExists(name)) {
			showErrorMessage("A room with that name already exists.");
			return false;
		}
		
		return true;
	}
	
	private void showErrorMessage(String errorMessage) {
		JOptionPane.showMessageDialog(this, errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
	}
	
	private void initialiseUI() {
		setTitle("Add Meeting Room");
		setMinimumSize(new Dimension(314, 200));
		getContentPane().setLayout(null);
		setLocationRelativeTo(null);
		JLabel lblAddNewMeeting = new JLabel("Add New Meeting Room");
		lblAddNewMeeting.setBounds(25, 11, 191, 22);
		lblAddNewMeeting.setFont(new Font("Tahoma", Font.PLAIN, 18));
		getContentPane().add(lblAddNewMeeting);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(51, 53, 39, 14);
		getContentPane().add(lblName);
		
		txtRoomName = new JTextField();
		txtRoomName.setBounds(95, 54, 158, 20);
		getContentPane().add(txtRoomName);
		txtRoomName.setColumns(10);
		
		JLabel lblLocation = new JLabel("Location");
		lblLocation.setBounds(35, 78, 60, 14);
		getContentPane().add(lblLocation);
		
		txtLocation = new JTextField();
		txtLocation.setBounds(95, 79, 158, 20);
		getContentPane().add(txtLocation);
		txtLocation.setColumns(10);
		
		JLabel lblCapacity = new JLabel("Capacity");
		lblCapacity.setBounds(36, 103, 58, 14);
		getContentPane().add(lblCapacity);
		
		txtCapacity = new JTextField();
		txtCapacity.setBounds(95, 104, 158, 20);
		getContentPane().add(txtCapacity);
		txtCapacity.setColumns(10);
		
		JButton btnSave = new JButton("Save");
		btnSave.setBounds(95, 131, 76, 23);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createMeetingRoom();
			}
		});
		getContentPane().add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(177, 131, 76, 23);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				hideForm();
			}
		});
		getContentPane().add(btnCancel);
	}

}
